﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2012/3/10 11:11:53
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sector7.Restaurant.EntityType;
using Sector7.Framework.Core;
using System.Data.SqlClient;
using Sector7.Restaurant.DAO.Utility;
using Sector7.Framework.DB;

namespace Sector7.Restaurant.DAO.OleDB
{
    /// <summary>
    /// 元数据类型数据访问接口
    /// </summary>
    public class MetadataTypeDAO : IMetadataTypeDAO
    {
        #region Public functions
        public int Create(RS_MetadataType type)
        {
            using (DBHelper opt = new DBHelper())
            {
                string sql = @"
                    INSERT INTO RS_MetadataType
                            (TypeName
                            ,Usage)
                        VALUES
                            (@TypeName
                            ,@Usage)
                    SELECT @@IDENTITY";

                return Convert.ToInt32(opt.ExecuteScalar(
                    sql,
                    DBUtil.CreateSqlParameter(type.TypeName, "TypeName"),
                    DBUtil.CreateSqlParameter(type.Usage, "Usage")));
            }
        }

        public List<RS_MetadataType> ReadAll(bool isGreedy)
        {
            using (DBHelper opt = new DBHelper())
            {
                List<RS_MetadataType> types = new List<RS_MetadataType>();
                string sql = @"
                    SELECT *
                        FROM RS_MetadataType";

                SqlDataReader reader = opt.ExecuteQuery(
                    sql);

                while (reader.Read())
                {
                    object type = new RS_MetadataType();

                    DBUtil.ReadData(reader, ref type);

                    if (isGreedy)
                    {
                        IMetadataDAO metadataDAO = new MetadataDAO();
                        (type as RS_MetadataType).Metadatas = metadataDAO.ReadAll((type as RS_MetadataType).TypeId);
                    }

                    types.Add(type as RS_MetadataType);
                }

                return types;
            }
        }

        public List<RS_MetadataType> ReadAll(int usage, bool isGreedy)
        {
            using (DBHelper opt = new DBHelper())
            {
                List<RS_MetadataType> types = new List<RS_MetadataType>();
                string sql = string.Format(@"
                    SELECT *
                        FROM RS_MetadataType
                        WHERE (Usage & {0}) = {0}", usage);

                SqlDataReader reader = opt.ExecuteQuery(
                    sql);

                while (reader.Read())
                {
                    object type = new RS_MetadataType();

                    DBUtil.ReadData(reader, ref type);

                    if (isGreedy)
                    {
                        IMetadataDAO metadataDAO = new MetadataDAO();
                        (type as RS_MetadataType).Metadatas = metadataDAO.ReadAll((type as RS_MetadataType).TypeId);
                    }

                    types.Add(type as RS_MetadataType);
                }

                return types;
            }
        }

        public int Update(EntityDelta delta)
        {
            using (DBHelper opt = new DBHelper())
            {
                return opt.ExecuteNonQuery(
                    DBUtil.CreateUpdateSql(delta),
                    DBUtil.CreateUpdateParameters(delta));
            }
        }

        public DBOptResultType Delete(int typeId)
        {
            using (DBHelper opt = new DBHelper())
            {
                string sql =
                    "DELETE FROM RS_MetadataType    " +
                    "   WHERE TypeId = @TypeId      ";

                return (DBOptResultType)opt.ExecuteNonQuery(
                    sql,
                    DBUtil.CreateSqlParameter(typeId, "TypeId"));
            }
        }
        #endregion
    }
}
